function getValue(id) {
    $("#" + id).children("div").children("p.alert-danger").text("");
    if ($("#" + id).find("li.active").length === 0) {
        return undefined;
    }
    else {
        return li = $("#" + id).find("li.active").children("input").val();
    }
}
$(document).ready(function() {
    $("ul.dropdown-menu").children("li").click(function() {
        $(this).parents("ul").children("li").removeClass("active");
        $(this).addClass("active");
        $(this).parent("ul").prev("button").html($(this).html() + "<span class=\"caret\">");
        $(this).parent("ul").parent("div").next("p").text("");
    });
    $("#formSearch").submit(function(event) {
        event.preventDefault();
        var name = $("#inputProductName").val();
        var manufacturer = getValue("manufacturers");
        var active = $("#active").val();
        var priceFrom = $("#priceFrom").val();
        var priceTo = $("#priceTo").val();
        var category = getValue("categories");
        var shopname = $("#shopname").val();
        var url = "/" + shopname + "/search/results";
        url += "?active=" + active;
        if (name) {
            url += "&name=" + name;
        }
        if (manufacturer) {
            url += "&man=" + manufacturer;
        }
        if (priceFrom) {
            url += "&from=" + priceFrom;
        }
        if (priceTo) {
            url += "&to=" + priceTo;
        }
        if (category) {
            url += "&cat=" + category;
        }
        window.location.href = url;
    });
});